package cn.hellochaos.generator.controller;


import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import cn.hellochaos.generator.entity.dto.ResultBean;
import cn.hellochaos.generator.service.PointService;
import cn.hellochaos.generator.entity.Point;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

import static cn.hellochaos.generator.entity.dto.ResultCode.MSG_RESOURCE_NOT_FOUND;
import static cn.hellochaos.generator.entity.dto.ResultCode.RESOURCE_NOT_FOUND;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fmy
 * @since 2021-07-16
 * @version v1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/fishery/api/v1/point")
public class PointController {

    @Resource
    private PointService pointService;

    /**
    * 查询分页数据
    */
    @RequestMapping(method = RequestMethod.GET)
    public ResultBean<?> listByPage(@RequestParam(name = "page", defaultValue = "1") int page,
                                    @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                    @RequestParam(name = "factor", defaultValue = "") String factor) {
        return new ResultBean<>(pointService.listPointsByPage(page, pageSize,factor));
    }

    /**
    * 根据id查询
    */
    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public ResultBean<?> getById(@PathVariable("id") Integer id) {
        Point point = pointService.getPointById(id);
        if (point==null){
            return new ResultBean<>(MSG_RESOURCE_NOT_FOUND,RESOURCE_NOT_FOUND,null);
        }
        return new ResultBean<>(point);
    }

    /**
    * 新增
    */
    @RequestMapping(method = RequestMethod.POST)
    public ResultBean<?> insert(@RequestBody Point point) {
        return new ResultBean<>(pointService.insertPoint(point));
    }

    /**
    * 删除
    */
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public ResultBean<?> deleteById(@PathVariable("id") Integer id) {

        int deletedId = pointService.deletePointById(id);

        if (deletedId==-1){
            return new ResultBean<>(MSG_RESOURCE_NOT_FOUND,RESOURCE_NOT_FOUND,null);
        }
        return new ResultBean<>(deletedId);
    }

    /**
    * 修改
    */
    @RequestMapping(method = RequestMethod.PUT)
    public ResultBean<?> updateById(@RequestBody Point point) {
        
        int updatedId = pointService.updatePoint(point);

        if (updatedId==-1){
            return new ResultBean<>(MSG_RESOURCE_NOT_FOUND,RESOURCE_NOT_FOUND,null);
        }
        return new ResultBean<>(updatedId);
    }
}
